package com.lb.jwitter.action;


 import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
 import java.util.Collection;
import java.util.Date;
import java.util.Dictionary;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;

 import org.springframework.beans.factory.annotation.Autowired;
import com.lb.jwitter.util.ApplicationContextProvider; 
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; 
import org.springframework.test.context.ContextConfiguration;
import org.springframework.context.support.ClassPathXmlApplicationContext;
 import com.opensymphony.xwork2.Action; 
 import java.util.Map; 
 import com.opensymphony.xwork2.ActionContext; 
import com.lb.jwitter.service.ConditionReport; 
import com.lb.jwitter.service.TblQLDTTXAHocVienDangKyMonHocService;
 import com.lb.jwitter.service.TblQuyenChiTietService;import com.lb.jwitter.service.TblTaiKhoan;import com.lb.jwitter.service.TblQLDTTXALopMonHocDuKien;
 import com.lb.jwitter.service.TblQLDTTXALopMonHocDuKienService;
 import com.lb.jwitter.service.TblQLDTTXAMonHoc;
 import com.lb.jwitter.service.TblQLDTTXAMonHocService;

import com.lb.jwitter.service.TblQLDTTXAHocKyTrongNam;
 import com.lb.jwitter.service.TblQLDTTXAHocKyTrongNamService;

import com.lb.jwitter.service.TblQLDTTXAGiaoVien;
import com.lb.jwitter.service.TblQLDTTXAGiaoVienService;

 /** 
 * @author QUOCVIET  
 * 
 */ 


@ContextConfiguration(locations="classpath:beans.xml") 
public class TblQLDTTXALopMonHocDuKienFormAction{
    private int pageSize; 
    public int getPageSize() {
       return pageSize;
 }

    public void setPageSize(int pageSize) { this.pageSize = pageSize; }

    private int page; 
    public int getPage() { return page; }

    public void setPage(int page) { this.page = page; }

    private int pages; 
    public int getPages() { return pages; }

    public void setPages(int pages) { this.pages = pages; }

    private String dataString; 
    public String getDataString() { return dataString; }
    private Map<Long, Integer> soLuongHocVien;
    public Map<Long, Integer> getSoLuongHocVien(){
    	return soLuongHocVien;
    }
    private List<TblQLDTTXALopMonHocDuKien> tblQLDTTXALopMonHocDuKiens; 
    public List<TblQLDTTXALopMonHocDuKien>  getTblQLDTTXALopMonHocDuKiens() {
         return tblQLDTTXALopMonHocDuKiens; 
      } 

    private TblQLDTTXALopMonHocDuKienService tblQLDTTXALopMonHocDuKienService; 

    private List<TblQLDTTXAMonHoc> tblQLDTTXAMonHocs; 
    public List<TblQLDTTXAMonHoc>  getTblQLDTTXAMonHocs(){ 
              return tblQLDTTXAMonHocs; 
     } 

    private TblQLDTTXAMonHocService tblQLDTTXAMonHocService; 

    private List<TblQLDTTXAHocKyTrongNam> tblQLDTTXAHocKyTrongNams; 
    public List<TblQLDTTXAHocKyTrongNam>  getTblQLDTTXAHocKyTrongNams(){ 
              return tblQLDTTXAHocKyTrongNams; 
     } 

    private TblQLDTTXAHocKyTrongNamService tblQLDTTXAHocKyTrongNamService; 

    private List<TblQLDTTXAGiaoVien> tblQLDTTXAGiaoViens; 
    public List<TblQLDTTXAGiaoVien>  getTblQLDTTXAGiaoViens(){ 
              return tblQLDTTXAGiaoViens; 
     } 

    private TblQLDTTXAGiaoVienService tblQLDTTXAGiaoVienService; 
    private TblQLDTTXAHocVienDangKyMonHocService tblQLDTTXAHocVienDangKyMonHocService;
    private TblQLDTTXAHocKyTrongNam tblQLDTTXAHocKyTrongNam;

  private TblTaiKhoan taiKhoanDangNhap; 
 public TblTaiKhoan getTaiKhoanDangNhap() { 
     return taiKhoanDangNhap; 
 } 
 public void setTaiKhoanDangNhap(TblTaiKhoan taiKhoanDangNhap) { 
     this.taiKhoanDangNhap = taiKhoanDangNhap; 
 } 
 private TblQuyenChiTietService tblQuyenChiTietService;
	 @Autowired // Injected by Spring 
     ApplicationContextProvider contextProvider; 
 public String execute() throws Exception 
{ 
 try{ 	
     Map session = ActionContext.getContext().getSession();
     taiKhoanDangNhap =  (TblTaiKhoan) session.get("taiKhoan");
     if( taiKhoanDangNhap == null) return "noprivilege";
    ApplicationContext context = ApplicationContextProvider.getApplicationContext(); 
    tblQuyenChiTietService = (TblQuyenChiTietService) context.getBean("tbl_Quyen_ChiTietClient"); 
    if(!tblQuyenChiTietService.checkRight(taiKhoanDangNhap.getMaNhomQuyen().getId(), "tbl_QLDTTXA_LopMonHoc_DuKien", "xem")){ 
        return "noprivilege"; 
    }tblQLDTTXALopMonHocDuKienService = (TblQLDTTXALopMonHocDuKienService) context.getBean("tbl_QLDTTXA_LopMonHoc_DuKienClient"); 
         tblQLDTTXAMonHocService = (TblQLDTTXAMonHocService) context.getBean("tbl_QLDTTXA_MonHocClient"); 

        tblQLDTTXAHocKyTrongNamService = (TblQLDTTXAHocKyTrongNamService) context.getBean("tbl_QLDTTXA_HocKyTrongNamClient"); 
        tblQLDTTXAHocVienDangKyMonHocService = (TblQLDTTXAHocVienDangKyMonHocService) context.getBean("tbl_QLDTTXA_HocVien_DangKyMonHocClient"); 
        
        tblQLDTTXAGiaoVienService = (TblQLDTTXAGiaoVienService) context.getBean("tbl_QLDTTXA_GiaoVienClient"); 
        soLuongHocVien = new HashMap<Long, Integer>();
       if(pageSize == 0){ 
            pageSize = 20; 
        } 
        if(page == 0){ 
            page =1; 
        } 
        
        
        
        Date today = new Date();
        List<ConditionReport> cons = new ArrayList<ConditionReport>(); 
        if(true){
        	DateFormat df = new SimpleDateFormat("dd/MM/yyyy"); 
        	
        	
        	ConditionReport con = new ConditionReport(); 
            con.setCol("thoiGianBatDau");
            con.setValue1(df.format(today));
            con.setOperator("le");
            con.setType("date");
            cons.add(con);	
            
            ConditionReport con2 = new ConditionReport(); 
            con2.setCol("thoiGianKetThuc");
            con2.setValue1(df.format(today));
            con2.setOperator("ge");
            con2.setType("date");
            cons.add(con2);	
        }
        tblQLDTTXAHocKyTrongNams = tblQLDTTXAHocKyTrongNamService.findtblQLDTTXAHocKyTrongNamByCondition(cons, 0, 0);

        if(tblQLDTTXAHocKyTrongNams != null && tblQLDTTXAHocKyTrongNams.size() ==1){ 
            tblQLDTTXAHocKyTrongNam = tblQLDTTXAHocKyTrongNams.get(0);
        }
        cons.clear();
        if(tblQLDTTXAHocKyTrongNams != null && tblQLDTTXAHocKyTrongNams.size() ==1){ 
            ConditionReport con = new ConditionReport(); 
            con.setCol("hocKyId.id");
            con.setValue1(tblQLDTTXAHocKyTrongNams.get(0).getId());
            con.setOperator("eq");
            cons.add(con);	
            
            ConditionReport con2 = new ConditionReport(); 
            con2.setCol("duocMo");
            con2.setValue1(false);
            con2.setOperator("eq");
            cons.add(con2);	
        }else{
        	ConditionReport con = new ConditionReport(); 
            con.setCol("hocKyId.id");
            con.setValue1("");
            con.setOperator("max");
            cons.add(con);	
            
            
            ConditionReport con2 = new ConditionReport(); 
            con2.setCol("duocMo");
            con2.setValue1(false);
            con2.setOperator("eq");
            cons.add(con2);	
        	
        }
        tblQLDTTXALopMonHocDuKiens = tblQLDTTXALopMonHocDuKienService.findtblQLDTTXALopMonHocDuKienByCondition(cons, 0, 0);
        
        for (TblQLDTTXALopMonHocDuKien monHoc : tblQLDTTXALopMonHocDuKiens){
        	List<ConditionReport> cons1 = new ArrayList<ConditionReport>(); 
            Long monHocId = monHoc.getMonHocId().getId();
       
        	ConditionReport con1 = new ConditionReport(); 
            con1.setCol("monHocId.id");
            con1.setValue1(monHocId);
            con1.setOperator("eq");
            cons1.add(con1);	
    	
            
            ConditionReport con2 = new ConditionReport(); 
            con2.setCol("phieuDangKyId.hocKyId.id");
            con2.setValue1(monHoc.getHocKyId().getId());
            con2.setOperator("eq");
            cons1.add(con2);	
                
           
            ConditionReport con3 = new ConditionReport(); 
            con3.setCol("daXepLop");
            con3.setValue1(false);
            con3.setOperator("eq");
            cons1.add(con3);	
            
            int soLuong = tblQLDTTXAHocVienDangKyMonHocService.counttblQLDTTXAHocVienDangKyMonHocByCondition(cons1);
            
            soLuongHocVien.put(monHocId, soLuong);            
        }
        pages = tblQLDTTXALopMonHocDuKienService.counttblQLDTTXALopMonHocDuKienByCondition(cons)/pageSize + 1; 
        
      dataString = ""; 
      dataString += "&monHocId=" ; 
      try{
    	  dataString += "&hocKyId=" +tblQLDTTXAHocKyTrongNam.getId() ; 
      }catch(Exception e){}
      dataString += "&giaoVienId=" ; 
      
      tblQLDTTXAMonHocs = tblQLDTTXAMonHocService.findAlltblQLDTTXAMonHoc(0, 0);
      tblQLDTTXAHocKyTrongNams = tblQLDTTXAHocKyTrongNamService.findAlltblQLDTTXAHocKyTrongNam(0, 0);
      
      tblQLDTTXAGiaoViens = tblQLDTTXAGiaoVienService.findAlltblQLDTTXAGiaoVien(0, 0);
        return Action.SUCCESS; 
      } 
      catch(Exception e){ 
      System.out.println("Loi Form tbl_QLDTTXA_LopMonHoc_DuKien Action"); 
     e.printStackTrace(); 
    return Action.ERROR; 
 } 
 }

public TblQLDTTXAHocKyTrongNam getTblQLDTTXAHocKyTrongNam() {
	return tblQLDTTXAHocKyTrongNam;
} 
 } 
 
